package com.example.a02back.controller;

import com.example.a02back.service.impl.UserServiceImpl;
import com.example.a02back.common.APIResponse;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName LoginController
 * @Description
 * @Author SimpChaos
 * @Date 2022/7/5 14:49
 * @Version 1.0
 **/

@RestController
@ResponseBody
@RequestMapping("/wx")
public class LoginController {

    @Resource
    private UserServiceImpl userService;

    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/7 16:26
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/session")
    public APIResponse<?> getSession(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){

        String code = httpServletRequest.getParameter("code");
        return APIResponse.success("success",userService.authorize(code));
    }

/**
 * @Description:
 * @Author: SimpChaos 
 * @Date: 2022/7/7 16:30
 * @Param: [httpServletRequest, httpServletResponse]
 * @Return: com.example.a02back.common.APIResponse<?>
 */
@CrossOrigin
@RequestMapping("/login")
    public APIResponse<?> login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String sessionId = httpServletRequest.getParameter("sessionId");
        String encryptedData = httpServletRequest.getParameter("encryptedData");
        String iv = httpServletRequest.getParameter("iv");
        return userService.login(sessionId,encryptedData,iv);
    }

    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/7 16:30
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/Login")
    public APIResponse<?> Login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String sessionId = httpServletRequest.getParameter("code");
        String encryptedData = httpServletRequest.getParameter("nickName");
        String iv = httpServletRequest.getParameter("avatarUrl");
        return userService.Login(sessionId,encryptedData,iv);
    }




}
